package pl.lsierant.j2me.bipacz.notifications.impl;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

import pl.lsierant.j2me.bipacz.notifications.INotification;
import pl.lsierant.j2me.bipacz.notifications.NotificationType;
import pl.lsierant.j2me.bipacz.persistent.DeserializationHelper;
import pl.lsierant.j2me.bipacz.persistent.IPersistent;
import pl.lsierant.j2me.bipacz.persistent.PersistentObjectType;
import pl.lsierant.j2me.bipacz.persistent.SerializationHelper;

/**
 * Vibrate notification over specified time duration.
 */
public class VibrateNotification implements INotification, IPersistent {
    // duration in milliseconds;
    private int duration = -1;

    /**
     * Creates vibrate notification with duration time.
     * 
     * @param duration
     *            vibration time in milliseconds
     */
    public VibrateNotification(int duration) {
        super();
        this.duration = duration;
    }

    /**
     * Creates vibrate notification with default duration time.
     */
    public VibrateNotification() {
        super();
    }

    public int getNotificationType() {
        return NotificationType.BEEP;
    }

    public void run() {
        NotificationManager.getInstance().run(this);
    }

    public int getDuration() {
        return duration;
    }

    public void initialize(DataInputStream serializedData) throws IOException {
        DeserializationHelper helper = new DeserializationHelper(serializedData);
        duration = ((Integer) helper.readObject()).intValue();
    }

    public void serialize(DataOutputStream out) throws IOException {
        SerializationHelper helper = new SerializationHelper(out, this, 0);
        helper.addField(duration);
    }

    public int getPersistentObjectType() {
        return PersistentObjectType.VIBRATE_NOTIFICATION;
    }

    public String toString() {
        return "VibrateNotification [duration=" + duration + "]";
    }

}
